<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>docker使用分享 - Foryun - Share</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Foryun" /><meta name="description" content="0.背景 一、环境配置的难题 软件开发最大的麻烦事之一，就是环境配置。用户计算机的环境都不相同，你怎么知道自家的软件，能在那些机器跑起来？ 用户必" /><meta name="keywords" content="Blog, IT, Share, Record" />






<meta name="generator" content="Hugo 0.81.0 with theme even" />


<link rel="canonical" href="http://foryun.com.cn/post/share/docker/docker%E4%BD%BF%E7%94%A8%E5%88%86%E4%BA%AB/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<link href="/sass/main.min.af7fd1da18d66c2b017df5b4cae508ef44cfcac3fb4c7c7a327fe4f4f9e28b08.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="docker使用分享" />
<meta property="og:description" content="0.背景 一、环境配置的难题 软件开发最大的麻烦事之一，就是环境配置。用户计算机的环境都不相同，你怎么知道自家的软件，能在那些机器跑起来？ 用户必" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://foryun.com.cn/post/share/docker/docker%E4%BD%BF%E7%94%A8%E5%88%86%E4%BA%AB/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2020-09-25T00:00:00&#43;00:00" />
<meta property="article:modified_time" content="2020-09-25T00:00:00&#43;00:00" />

<meta itemprop="name" content="docker使用分享">
<meta itemprop="description" content="0.背景 一、环境配置的难题 软件开发最大的麻烦事之一，就是环境配置。用户计算机的环境都不相同，你怎么知道自家的软件，能在那些机器跑起来？ 用户必"><meta itemprop="datePublished" content="2020-09-25T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-09-25T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="2888">
<meta itemprop="keywords" content="docker," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="docker使用分享"/>
<meta name="twitter:description" content="0.背景 一、环境配置的难题 软件开发最大的麻烦事之一，就是环境配置。用户计算机的环境都不相同，你怎么知道自家的软件，能在那些机器跑起来？ 用户必"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Foryun</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/diary/">
        <li class="mobile-menu-item">Diary</li>
      </a><a href="/record/">
        <li class="mobile-menu-item">Record</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Foryun</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/diary/">Diary</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/record/">Record</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">docker使用分享</h1>

      <div class="post-meta">
        <span class="post-time"> 2020-09-25 </span>
        <div class="post-category">
            <a href="/categories/docker/"> docker </a>
            </div>
          <span class="more-meta"> 约 2888 字 </span>
          <span class="more-meta"> 预计阅读 6 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li>
          <ul>
            <li><a href="#0背景">0.背景</a></li>
            <li><a href="#1环境安装mac版">1.环境安装（Mac版）</a></li>
            <li><a href="#2构建目录结构">2.构建目录结构</a></li>
            <li><a href="#3创建go文件">3.创建go文件</a></li>
            <li><a href="#4编写go代码">4.编写go代码</a></li>
            <li><a href="#5创建dockerfile">5.创建dockerfile</a></li>
            <li><a href="#6编写dockerfile">6.编写dockerfile</a></li>
            <li><a href="#7执行docker命令">7.执行docker命令</a></li>
            <li><a href="#8执行go命令">8.执行go命令</a></li>
            <li><a href="#9查看运行结果">9.查看运行结果</a></li>
            <li><a href="#10移除容器和镜像">10.移除容器和镜像</a></li>
            <li><a href="#11编写docker-compose">11.编写docker-compose</a></li>
            <li><a href="#12运行docker-compose">12.运行docker-compose</a></li>
            <li><a href="#13保存加载镜像">13.保存、加载镜像</a></li>
            <li><a href="#14上传镜像到docker-hub">14.上传镜像到docker hub</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h3 id="0背景">0.背景</h3>
<h4 id="一环境配置的难题">一、环境配置的难题</h4>
<p>软件开发最大的麻烦事之一，就是环境配置。用户计算机的环境都不相同，你怎么知道自家的软件，能在那些机器跑起来？</p>
<p>用户必须保证两件事：操作系统的设置，各种库和组件的安装。只有它们都正确，软件才能运行。举例来说，安装一个 Python 应用，计算机必须有 Python 引擎，还必须有各种依赖，可能还要配置环境变量。</p>
<p>如果某些老旧的模块与当前环境不兼容，那就麻烦了。开发者常常会说：&ldquo;它在我的机器可以跑了&rdquo;（It works on my machine），言下之意就是，其他机器很可能跑不了。</p>
<p>环境配置如此麻烦，换一台机器，就要重来一次，旷日费时。很多人想到，能不能从根本上解决问题，软件可以带环境安装？也就是说，安装的时候，把原始环境一模一样地复制过来。</p>
<h4 id="二虚拟机">二、虚拟机</h4>
<p>虚拟机（virtual machine）就是带环境安装的一种解决方案。它可以在一种操作系统里面运行另一种操作系统，比如在 Windows 系统里面运行 Linux 系统。应用程序对此毫无感知，因为虚拟机看上去跟真实系统一模一样，而对于底层系统来说，虚拟机就是一个普通文件，不需要了就删掉，对其他部分毫无影响。</p>
<p>虽然用户可以通过虚拟机还原软件的原始环境。但是，这个方案有几个缺点。</p>
<p><strong>（1）资源占用多</strong></p>
<p>虚拟机会独占一部分内存和硬盘空间。它运行的时候，其他程序就不能使用这些资源了。哪怕虚拟机里面的应用程序，真正使用的内存只有 1MB，虚拟机依然需要几百 MB 的内存才能运行。</p>
<p><strong>（2）冗余步骤多</strong></p>
<p>虚拟机是完整的操作系统，一些系统级别的操作步骤，往往无法跳过，比如用户登录。</p>
<p><strong>（3）启动慢</strong></p>
<p>启动操作系统需要多久，启动虚拟机就需要多久。可能要等几分钟，应用程序才能真正运行。</p>
<h4 id="三linux-容器">三、Linux 容器</h4>
<p>由于虚拟机存在这些缺点，Linux 发展出了另一种虚拟化技术：Linux 容器（Linux Containers，缩写为 LXC）。</p>
<p>**Linux 容器不是模拟一个完整的操作系统，而是对进程进行隔离。**或者说，在正常进程的外面套了一个<a href="https://opensource.com/article/18/1/history-low-level-container-runtimes">保护层</a>。对于容器里面的进程来说，它接触到的各种资源都是虚拟的，从而实现与底层系统的隔离。</p>
<p>由于容器是进程级别的，相比虚拟机有很多优势。</p>
<p><strong>（1）启动快</strong></p>
<p>容器里面的应用，直接就是底层系统的一个进程，而不是虚拟机内部的进程。所以，启动容器相当于启动本机的一个进程，而不是启动一个操作系统，速度就快很多。</p>
<p><strong>（2）资源占用少</strong></p>
<p>容器只占用需要的资源，不占用那些没有用到的资源；虚拟机由于是完整的操作系统，不可避免要占用所有资源。另外，多个容器可以共享资源，虚拟机都是独享资源。</p>
<p><strong>（3）体积小</strong></p>
<p>容器只要包含用到的组件即可，而虚拟机是整个操作系统的打包，所以容器文件比虚拟机文件要小很多。</p>
<p>总之，容器有点像轻量级的虚拟机，能够提供虚拟化的环境，但是成本开销小得多。</p>
<h4 id="四docker-是什么">四、Docker 是什么？</h4>
<p>**Docker 属于 Linux 容器的一种封装，提供简单易用的容器使用接口。**它是目前最流行的 Linux 容器解决方案。</p>
<p>Docker 将应用程序与该程序的依赖，打包在一个文件里面。运行这个文件，就会生成一个虚拟容器。程序在这个虚拟容器里运行，就好像在真实的物理机上运行一样。有了 Docker，就不用担心环境问题。</p>
<p>总体来说，Docker 的接口相当简单，用户可以方便地创建和使用容器，把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改，就像管理普通的代码一样。</p>
<h4 id="五docker-的用途">五、Docker 的用途</h4>
<p>Docker 的主要用途，目前有三大类。</p>
<p>**（1）提供一次性的环境。**比如，本地测试他人的软件、持续集成的时候提供单元测试和构建的环境。</p>
<p>**（2）提供弹性的云服务。**因为 Docker 容器可以随开随关，很适合动态扩容和缩容。</p>
<p>**（3）组建微服务架构。**通过多个容器，一台机器可以跑多个服务，因此在本机就可以模拟出微服务架构。</p>
<h3 id="1环境安装mac版">1.环境安装（Mac版）</h3>
<ul>
<li>链接: <a href="https://pan.baidu.com/s/1Jdj2ZmeX1TTsuNnxwzP0iQ">https://pan.baidu.com/s/1Jdj2ZmeX1TTsuNnxwzP0iQ</a>  密码: 65l0</li>
</ul>
<h3 id="2构建目录结构">2.构建目录结构</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">cd ~
mkdir -p docker-home/go-web/golang/src
</code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>cd ~ 是进入当前用户目录下</p>
</li>
<li>
<p>mkdir -p 是创建多级目录</p>
</li>
</ul>
<h3 id="3创建go文件">3.创建go文件</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">cd docker-home/go-web/golang/src
touch main.go
open main.go
</code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>touch 创建文件</p>
</li>
<li>
<p>open 打开文件</p>
</li>
</ul>
<h3 id="4编写go代码">4.编写go代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
    <span class="s">&#34;net/http&#34;</span>
    <span class="s">&#34;fmt&#34;</span>
    <span class="s">&#34;log&#34;</span>
<span class="p">)</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">http</span><span class="p">.</span><span class="nf">HandleFunc</span><span class="p">(</span><span class="s">&#34;/&#34;</span><span class="p">,</span> <span class="kd">func</span><span class="p">(</span><span class="nx">writer</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">request</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">fmt</span><span class="p">.</span><span class="nf">Fprint</span><span class="p">(</span><span class="nx">writer</span><span class="p">,</span> <span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
    <span class="p">})</span>
    <span class="nx">log</span><span class="p">.</span><span class="nf">Fatal</span><span class="p">(</span><span class="nx">http</span><span class="p">.</span><span class="nf">ListenAndServe</span><span class="p">(</span><span class="s">&#34;:3000&#34;</span><span class="p">,</span><span class="kc">nil</span><span class="p">))</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5创建dockerfile">5.创建dockerfile</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">cd ~
cd docker-home/go-web/golang
touch Dockerfile
</code></pre></td></tr></table>
</div>
</div><h3 id="6编写dockerfile">6.编写dockerfile</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dockerfile" data-lang="dockerfile"><span class="c"># 使用docker hub上的golang，版本为1.15.1</span><span class="err">
</span><span class="err"></span><span class="k">FROM</span><span class="s"> golang:1.15.2</span><span class="err">
</span><span class="err"></span><span class="c"># 拷贝当前目下src文件下的所有文件到go环境的src文件下</span><span class="err">
</span><span class="err"></span><span class="k">COPY</span> ./src ./src<span class="err">
</span><span class="err"></span><span class="c"># 暴露3000端口</span><span class="err">
</span><span class="err"></span><span class="k">EXPOSE</span><span class="s"> 3000</span><span class="err">
</span></code></pre></td></tr></table>
</div>
</div><h3 id="7执行docker命令">7.执行docker命令</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker build -t go-web .
docker images
docker run -it -p 3000:3000 --rm --name go-web-running go-web
</code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>docker build -t go-web . 在当前目录寻找Dockerfile构建一个tag是go-web的镜像</p>
<ul>
<li>-t 是给镜像添加tag，如果不写，则会随机一个tag</li>
<li>. 是Dockerfile的目录</li>
</ul>
</li>
<li>
<p>docker images 显示本地的所有镜像</p>
</li>
<li>
<p>docker run -it -P &ndash;rm &ndash;name go-web-running go-web 用go-web构建名为go-web-running的容器</p>
<ul>
<li>-it 以交互模式运行容器，并启动伪输入终端</li>
<li>-p 指定端口映射，格式为：主机(宿主)端口:容器端口</li>
<li>&ndash;rm 容器运行结束后就移除</li>
<li>&ndash;name 设置容器的名字</li>
</ul>
</li>
</ul>
<h3 id="8执行go命令">8.执行go命令</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"><span class="nx">ls</span>
<span class="nx">cd</span> <span class="nx">src</span>
<span class="k">go</span> <span class="nx">build</span>
<span class="nx">ls</span>
<span class="p">.</span><span class="o">/</span><span class="nx">src</span>
</code></pre></td></tr></table>
</div>
</div><ul>
<li>执行了上面的docker的run命令后，进入到go的环境，因为脚本拷贝到了src目录下，所以要进入该目录，然后执行build操作，生成可执行文件。</li>
</ul>
<h3 id="9查看运行结果">9.查看运行结果</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker ps -a
</code></pre></td></tr></table>
</div>
</div><ul>
<li>docker ps -a 显示所有容器</li>
<li>在浏览器里面输入http://localhost:3000/</li>
<li>浏览器里面显示Hello World证明运行成功</li>
</ul>
<h3 id="10移除容器和镜像">10.移除容器和镜像</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">ctrl+c
exit
docker ps -a
docker images
docker image rm go-web
</code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>ctrl+c 退出运行的web</p>
</li>
<li>
<p>exit 退出go容器</p>
</li>
<li>
<p>docker image rm go-web 移除go-web镜像</p>
</li>
</ul>
<h3 id="11编写docker-compose">11.编写docker-compose</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">cd ~
cd docker-home/go-web
touch docker-compose.yml
open docker-compose.yml
</code></pre></td></tr></table>
</div>
</div><ul>
<li>在打开的docker-compose.yml中写入</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dockerfile" data-lang="dockerfile">version: <span class="s1">&#39;3&#39;</span><span class="err">
</span><span class="err">
</span><span class="err"></span>services:<span class="err">
</span><span class="err"></span>  go-web:<span class="err">
</span><span class="err"></span>    <span class="c1"># 镜像名</span><span class="err">
</span><span class="err"></span>    image: go-web<span class="err">
</span><span class="err"></span>    <span class="c1"># 容器名</span><span class="err">
</span><span class="err"></span>    container_name: go-web-running<span class="err">
</span><span class="err"></span>    <span class="c1"># 工作目录 启动后进入指定目录</span><span class="err">
</span><span class="err"></span>    working_dir: /go/src<span class="err">
</span><span class="err"></span>    <span class="c1"># 用指定目录下的Dockerfile构建镜像</span><span class="err">
</span><span class="err"></span>    build:<span class="err">
</span><span class="err"></span>      context: ./golang<span class="err">
</span><span class="err"></span>      dockerfile: Dockerfile<span class="err">
</span><span class="err"></span>    <span class="c1"># 挂载目录</span><span class="err">
</span><span class="err"></span>    volumes:<span class="err">
</span><span class="err"></span>      - ~/docker-home/go-web/golang/src:/src<span class="err">
</span><span class="err"></span>    <span class="c1"># 端口映射</span><span class="err">
</span><span class="err"></span>    ports:<span class="err">
</span><span class="err"></span>      - <span class="s2">&#34;3000:3000&#34;</span><span class="err">
</span><span class="err"></span>    <span class="c1"># 容器启动后执行的命令</span><span class="err">
</span><span class="err"></span>    command: bash -c <span class="s2">&#34;</span><span class="err">
</span><span class="err"></span>      go build <span class="o">&amp;&amp;</span><span class="err">
</span><span class="err"></span>      ./src<span class="err">
</span><span class="err"></span>      <span class="s2">&#34;</span><span class="err">
</span></code></pre></td></tr></table>
</div>
</div><h3 id="12运行docker-compose">12.运行docker-compose</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker-compose up -d
</code></pre></td></tr></table>
</div>
</div><ul>
<li>docker-compose up -d 在后台运行docker-compose</li>
<li>docker ps -a 显示所有容器</li>
<li>在浏览器里面输入http://localhost:3000/</li>
<li>浏览器里面显示Hello World证明运行成功</li>
</ul>
<h3 id="13保存加载镜像">13.保存、加载镜像</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker stop go-web-running
docker rm go-web-running
docker save go-web&gt;go-web.tar
ls
docker images
docker image rm go-web
docker load&lt;go-web.tar
docker images
</code></pre></td></tr></table>
</div>
</div><ul>
<li>docker stop go-web-running 停止go-web-running容器</li>
<li>docker save go-web&gt;go-web.tar 将go-web镜像写入go-web.tar中</li>
<li>docker load&lt;go-web.tar 加载go-web.tar</li>
</ul>
<h3 id="14上传镜像到docker-hub">14.上传镜像到docker hub</h3>
<ul>
<li>
<p>打开https://hub.docker.com/</p>
</li>
<li>
<p>注册账号</p>
</li>
<li>
<p>登录账号，在Repositories中点击Create Repository</p>
</li>
<li>
<p>name填写go-web，description填写go-web</p>
</li>
<li>
<p>点击Create</p>
</li>
<li>
<p>打开终端</p>
</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">docker login
输入用户名
输入密码
docker images
docker tag 96c5b7db67be chengjin0619/go-web
docker push chengjin0619/go-web
docker pull chengjin0619/go-web
</code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>docker login 登录到docker hub</p>
</li>
<li>
<p>docker tag 96c5b7db67be chengjin0619/go-web 给镜像id为96c5b7db67be的镜像打上chengjin0619/go-web的tag</p>
</li>
<li>
<p>docker push chengjin0619/go-web 将tag为chengjin0619/go-web的镜像推送到docker hub上</p>
</li>
<li>
<p>docker pull chengjin0619/go-web 从docker hub上拉取chengjin0619/go-web镜像</p>
</li>
</ul>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Foryun</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2020-09-25
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/docker/">docker</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/share/docker/docker%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB%E6%80%BB%E7%BB%93/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">docker技术分享总结</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/share/docker/dockerfile/">
            <span class="next-text nav-default">Dockerfile</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/share/docker/docker%E4%BD%BF%E7%94%A8%E5%88%86%E4%BA%AB/" class="leancloud_visitors" data-flag-title="docker使用分享">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'insXINA0D2ccQdcEfEcMSjgn-gzGzoHsz',
        appKey: 'HEAEkloDNn3aFg4Yr1W4iGM3',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:1186976350@qq.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/DanicCheng" class="iconfont icon-github" title="github"></a>
      <a href="https://space.bilibili.com/2920221?spm_id_from=333.788.b_765f7570696e666f.2" class="iconfont icon-bilibili" title="bilibili"></a>
  
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2017 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>Foryun</span>
  </span>

  <span class="beian">
    
    备案号：<a href="https://beian.miit.gov.cn" target="_blank">粤ICP备18045119号</a>
    
  </span>

</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?57ed19d6fdd95893edd119b86ac49dab";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
